package com.wanxi.servlet.manager;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.Impl.ManagerServiceImpl;
import com.wanxi.service.ManagerService;
import com.wanxi.vo.ManagerVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/admin/manager/add")
public class AddManagerServlet extends HttpServlet {
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
//    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        乱码处理、传输格式处理
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
//        获取前端的值
        String managerName=req.getParameter("managerName");
        String password=req.getParameter("password");
        Integer enable=null;
        if(req.getParameter("enable")!=null)
            enable=Integer.parseInt(req.getParameter("enable"));
        String remark=req.getParameter("remark");
//        封装
        ManagerVO managerVO= ManagerVO.builder()
                .managerName(managerName)
                .password(password)
                .enable(enable)
                .remark(remark)
                .build();
//        调用service处理
        ManagerService managerService =new ManagerServiceImpl();
        ResultDTO resultDTO= managerService.addManager(managerVO);
//        放回值给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
